package servlet.filtre;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class FiltreConnexion implements Filter {
	
    public void init( FilterConfig config ) throws ServletException {
    }

    public void doFilter( ServletRequest req, ServletResponse res, FilterChain chain ) throws IOException,
            ServletException {
    	 HttpServletRequest request = (HttpServletRequest) req;
         HttpServletResponse response = (HttpServletResponse) res;
         
         String nomPage = request.getServletPath().substring(1);
         if(!nomPage.equals("connection.html") && !nomPage.equals("inscription.html") ){
	         HttpSession session = request.getSession();
	         synchronized (session) {
				if(session.getAttribute("joueur")==null){
					response.sendRedirect(response.encodeRedirectURL("connection.html"));
					return;
				}
			}
	        
         }
         chain.doFilter( request, response );
    }

    public void destroy() {
        // ...
    }
}